package com.zhai.cloud.starter.quartz.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @package: com.example.quartzdemo.util
 * @project-name: zhai-cloud
 * @description: //TODO 请对该类编写一个描述
 * @author: Created by 张清泉
 * @create-datetime: 2019/7/16 13:56
 */
public class QuartzCronDateUtil {
    /***
     *  功能描述：日期转换cron表达式时间格式
     * @param date
     * @param dateFormat : e.g:yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String formatDateByPattern(Date date,String dateFormat){
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        String formatTimeStr = null;
        if (date != null) {
            formatTimeStr = sdf.format(date);
        }
        return formatTimeStr;
    }
    /***
     * convert Date to cron ,eg.  "14 01 17 22 07 ? 2017"
     * @param date:时间点
     * @return
     */
    public static String getCron(Date  date){
        String dateFormat="ss mm HH dd MM ? yyyy";
        return formatDateByPattern(date,dateFormat);
    }

    /***
     * convert Date to cron ,eg.  "14 01 17 22 07 ? 2017"
     * @param date:时间点
     * @return
     */
    public static String getCronTest(Integer  date){
        String dateFormat = date + " * * * * ?";
        return dateFormat;
    }

    /***
     * convert Date to cron ,eg.  "14 01 17 22 07 ? 2017"
     * @param date:时间点
     * @return
     */
    public static String getCron(Integer  date){
        String dateFormat = getSeconds() + " " + getSeconds() + " " + getHour() + " 1/" + date + " * ?";
        return dateFormat;
    }

    private static Integer getHour() {
        return new Random().nextInt(4);
    }

    private static Integer getSeconds() {
        return new Random().nextInt(60);
    }


    public static void main(String[] args) {
        for (int i =0 ; i< 100 ; i++) {
            System.out.println(getCron(new Random().nextInt(31) + 1 ));
        }
    }
}
